package com.sskj.gocoin.v.activity;


import android.support.v4.widget.NestedScrollView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.HttpData;
import com.sskj.gocoin.m.bean.XinXiBean;
import com.sskj.gocoin.m.http.JsonCallBack;
import com.sskj.gocoin.p.activity.WebActivityPresenter;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.RConfig;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.MINE_WEB)
public class WebActivity extends BaseActivity<WebActivityPresenter> {

    @BindView(R2.id.web_toolbar)
    ToolBarLayout mWebToolbar;
    @BindView(R2.id.tv_content)
    TextView tvContent;
    @BindView(R2.id.nestedScrollView)
    NestedScrollView nestedScrollView;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_web;
    }

    @Override
    protected void initData() {
        super.initData();
        ButterKnife.bind(this);
        String type = getIntent().getStringExtra(Constants.INTENT_TYPE);

        switch (type) {
            case "1":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0001);
                break;
            case "2":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0002);
                break;
            case "3":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0003);
                break;
            case "4":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0004);
                break;
            case "5":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0005);
                break;
            case "6":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0006);
                break;
            case "7":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0007);
                break;
            case "8":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0008);
                break;
            case "9":
                mWebToolbar.setTitle(R.string.gocoinWebActivity0009);
                break;
            case "10":
                mWebToolbar.setTitle(R.string.gocoinWebActivity00010);
                break;
            case "11":
                mWebToolbar.setTitle(R.string.gocoinWebActivity00011);
                break;
            case "12":
                mWebToolbar.setTitle(R.string.gocoinWebActivity00012);
                break;
            case "13":
                mWebToolbar.setTitle(R.string.gocoinWebActivity00013);
                break;
            case "14":
                mWebToolbar.setTitle(R.string.gocoinWebActivity00014);
                break;
            case "15":
                mWebToolbar.setTitle(R.string.gocoinWebActivity00015);
                break;
            default:
                mWebToolbar.setTitle(R.string.gocoinWebActivity00016);
                break;
        }

        mWebToolbar.setLeftButtonOnClickLinster(v -> finish());
        OkGo.<HttpData<XinXiBean>>get(HttpConfig.BASE_URL + HttpConfig.GET_XIEYI)
                .params("type", TextUtils.isEmpty(type) ? "user" : type)
                .execute(new JsonCallBack<HttpData<XinXiBean>>() {
                    @Override
                    public void _onSuccess(Response<HttpData<XinXiBean>> response) {
                        HttpData<XinXiBean> httpListData = response.body();
                        tvContent.setText(httpListData.getData() != null ? Html.fromHtml(httpListData.getData().getContent()) : "");
                    }
                });


        if ("15".equals(type)) {
            //科普知识
            nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                @Override
                public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    View onlyChild = nestedScrollView.getChildAt(0);
                    if (onlyChild.getHeight() <= scrollY + nestedScrollView.getHeight()) {

                        if (isFirstBottom) {
                            isFirstBottom = false;
                            // 如果满足就是到底部了
                            mPresenter.gameKepugot();
                        }

                    }
                }
            });
        }
    }

    boolean isFirstBottom = true;


    @Override
    public WebActivityPresenter getPresenter() {
        return new WebActivityPresenter();
    }


    public void updateUi(String reason) {
        ToastUtil.showLong(reason);
    }

    public void error(String msg) {
        ToastUtil.showLong(msg);
    }
}
